export function getPositionFn(hasMsg, message, detailLocationPlan, that) { // 平面上选取坐标点
  let picWidth = '';
  let picHeight = '';
  let moveX = 0, moveY = 0, x, y;
  let antModalBodyDiv = null;
  let layerX = 0, layerY = 0;
  if(!that.state.floorId) {

    message.warning('请先选择安装位置楼层', 1.5);
    that.setState({
      visible: false
    });
    return;
  }else {
    detailLocationPlan(that.state.floorId).then(data => {
      // hasMsg();
      if(data.floorPlan) {
        that.state.pos_span = document.getElementById('pos_span');
        that.state.planImg = document.getElementById('floor_plan');
        that.state.planImg.src = data.floorPlan;
        picWidth = that.state.planImg.width;
        picHeight = that.state.planImg.height;
        if(that.state.floorPlanXY[0] !== 0 && that.state.floorPlanXY[1] !== 0) {
          that.state.pos_span.classList.remove('none');
          that.state.pos_span.style.left = that.state.floorPlanXY[0] * that.state.planImg.width / 100 + 'px';
          that.state.pos_span.style.top = that.state.floorPlanXY[1] * that.state.planImg.height / 100 + 'px';
          layerX = that.state.floorPlanXY[0] * that.state.planImg.width / 100;
          layerY = that.state.floorPlanXY[1] * that.state.planImg.height / 100;
          that.state.planSpan.innerText = `平面坐标: ${that.state.floorPlanXY[0]}, ${that.state.floorPlanXY[1]}`;
        }
        that.state.planImg.addEventListener('click', (e) => {
          that.state.planSpan.innerText = '';
          that.state.layerX = e.layerX;
          that.state.layerY = e.layerY;
          layerX = that.state.layerX;
          layerY = that.state.layerY;
          that.state.pos_span.classList.remove('none');
          that.state.pos_span.style.left = that.state.layerX + 'px';
          that.state.pos_span.style.top = that.state.layerY + 'px';
          let ampX = null, ampY = null, absX = 0, absY = 0;
          absX = picWidth / 2 - e.layerX;
          absY = picHeight / 2 - e.layerY;
          if(absX > 0 && absY > 0) {
            ampX = (that.state.layerX + Math.abs(absX) * (that.state.multiple - 1 - moveX / picWidth * 2)).toFixed(0);
            ampY = (that.state.layerY + Math.abs(absY) * (that.state.multiple - 1 - moveY / picHeight * 2)).toFixed(0);
          }else if(absX > 0 && absY < 0){
            ampX = (that.state.layerX + Math.abs(absX) * (that.state.multiple - 1 - moveX / picWidth * 2)).toFixed(0);
            ampY = (that.state.layerY - Math.abs(absY) * (that.state.multiple - 1 - moveY / picHeight * 2)).toFixed(0);
          }else if(absX < 0 && absY > 0){
            ampX = (that.state.layerX - Math.abs(absX) * (that.state.multiple - 1 - moveX / picWidth * 2)).toFixed(0);
            ampY = (that.state.layerY + Math.abs(absY) * (that.state.multiple - 1 - moveY / picHeight * 2)).toFixed(0);
          }else{
            ampX = (that.state.layerX - Math.abs(absX) * (that.state.multiple - 1 - moveX / picWidth * 2)).toFixed(0);
            ampY = (that.state.layerY - Math.abs(absY) * (that.state.multiple - 1 - moveY / picHeight * 2)).toFixed(0);
          }
          if(that.state.multiple > 1) {
            that.state.layerX = ampX;
            that.state.layerY = ampY;
          }
          that.state.planSpan.innerText = `平面坐标: ${(that.state.layerX / picWidth * 100).toFixed(2)}, ${(that.state.layerY / picHeight * 100).toFixed(2)}`;
        });
      }else {
        message.warning('该楼层暂无平面图', 1.5);
        that.setState({
          visible: false
        });
      }
    });
  }
  setTimeout(() => {
    let antModal = document.querySelector('.ant-modal');
    let modalFooter = document.querySelector('.ant-modal-footer');
    let modalFooterDiv = document.querySelector('.ant-modal-footer div');
    let antModalBody = document.querySelector('.ant-modal-body');
    antModalBodyDiv = document.querySelector('.ant-modal-body div');
    let addDiv = document.createElement('div');
    let musDiv = document.createElement('div');
    if(antModal) {
      antModal.style.width = '800px';
      antModalBody.style.width = '90%';
      antModalBody.style.position = 'relative';
      antModalBody.style.userSelect = 'none';
      addDiv.style.position = 'absolute';
      addDiv.style.right = '-7%';
      addDiv.style.top = '50px';
      addDiv.style.padding = '10px 18px';
      addDiv.style.border = '1px solid #ccc';
      addDiv.style.borderRadius = '4px';
      addDiv.style.fontSize = '18px';
      addDiv.style.cursor = 'pointer';
      addDiv.style.zIndex = 99;
      addDiv.innerText = '+';

      musDiv.style.position = 'absolute';
      musDiv.style.right = '-7%';
      musDiv.style.top = '150px';
      musDiv.style.padding = '10px 19px';
      musDiv.style.border = '1px solid #ccc';
      musDiv.style.borderRadius = '4px';
      musDiv.style.fontSize = '18px';
      musDiv.style.cursor = 'pointer';
      musDiv.style.zIndex = 99;
      musDiv.innerText = '-';
      if(that.state.isChange) {
        antModalBody.appendChild(addDiv);
        antModalBody.appendChild(musDiv);
        that.state.planSpan = document.createElement('span');
        modalFooter.insertBefore(that.state.planSpan, modalFooterDiv);
      }
      modalFooter.style.display = 'flex';
      modalFooter.style.justifyContent = 'space-between';

      // 图片放大
      let antTime = null;
      addDiv.addEventListener('click', () => {
        if(typeof antTime === 'function') {
          antTime();
        }
        antModalBodyDiv.style.width = '100%';
        antTime = setTimeout(() => {
          antModalBodyDiv.style.width = 'auto';
        }, 0);
        if(that.state.multiple > 1.4) {
          return;
        }
        that.state.multiple += 0.2;
        x = picWidth / 2 - layerX;
        y = picHeight / 2 - layerY;
        that.state.planImg.style.transform = `scale(${that.state.multiple})`;
        moveX = Math.abs(x) * 0.16;
        moveY = Math.abs(y) * 0.16;
        if(x > 0 && y > 0) {
          layerX = layerX - moveX;
          layerY = layerY - moveY;
        }else if(x > 0 && y < 0) {
          layerX = layerX - moveX;
          layerY = layerY + moveY;
        }else if(x < 0 && y > 0) {
          layerX = layerX + moveX;
          layerY = layerY - moveY;
        }else {
          layerX = layerX + moveX;
          layerY = layerY + moveY;
        }
        that.state.pos_span.style.left = layerX + 'px';
        that.state.pos_span.style.top = layerY + 'px';
      });
      // 图片缩小
      let musTime = null;
      musDiv.addEventListener('click', () => {
        if(typeof musTime === 'function') {
          musTime();
        }
        musTime = setTimeout(() => {
          antModalBodyDiv.style.width = 'auto';
        }, 0);
        if(that.state.multiple <= 1) {
          antModalBodyDiv.style.width = '100%';
          return;
        }
        that.state.multiple -= 0.2;
        x = picWidth / 2 - layerX;
        y = picHeight / 2 - layerY;
        that.state.planImg.style.transform = `scale(${that.state.multiple})`;
        moveX = Math.abs(x) * 0.16;
        moveY = Math.abs(y) * 0.16;
        if(x > 0 && y > 0) {
          layerX = layerX + moveX;
          layerY = layerY + moveY;
        }else if(x > 0 && y < 0) {
          layerX = layerX + moveX;
          layerY = layerY - moveY;
        }else if(x < 0 && y > 0) {
          layerX = layerX - moveX;
          layerY = layerY + moveY;
        }else {
          layerX = layerX - moveX;
          layerY = layerY - moveY;
        }
        that.state.pos_span.style.left = layerX + 'px';
        that.state.pos_span.style.top = layerY + 'px';
      });
    }
  }, 10);
}

export function getBuildFn(level, dispatch, floorModelType, areaModelType, targetOption, locationData, that) {
  // 获取建筑里的区域
  switch(level) {
    case '1':
      dispatch({
        type: floorModelType,
        payload: {
          url: 'floor_plan/list',
          buildId: targetOption.id
        }
      }).then(() => {
        targetOption.loading = false;
        let { optionData = [] } = that.props;
        optionData.map(item => {
          item.label = item.floorName;
          item.value = item.id;
          if(areaModelType) {
            item.isLeaf = false;
          }
          item.level = '2';
          delete item.name;
          return item;
        });
        targetOption.children = optionData;
        locationData.map(item => {
          if(item.id === targetOption.id) {
            return targetOption;
          }
          return item;
        });
        that.setState({ option: locationData }, () => {
          const op = that.state.option;
          that.setState({
            option: [...op],
          });
        });
      });
      break;
    case '2':
      if(areaModelType) {
        that.state.floorId = targetOption.id;
        dispatch({
          type: areaModelType,
          payload: {
            url: 'area/list',
            floorId: targetOption.id
          }
        }).then(() => {
          targetOption.loading = false;
          let { areaData = [] } = that.props;
          areaData.map(item => {
            item.label = item.name;
            item.value = item.id;
            item.isLeaf = true;
            item.level = '3';
            delete item.name;
            return item;
          });
          targetOption.children = areaData;
          locationData.map(item => {
            if(item.id === targetOption.buildId) {
              return item.children.map(c_item => {
                if(c_item.floorId === targetOption.floorId) {
                  return targetOption;
                }
                return c_item;
              });
            }
            return item;
          });
          that.setState({ option: locationData }, () => {
            const op = that.state.option;
            that.setState({
              option: [...op],
            });
          });
        });
      }
      break;
    default:
      break;
  }
}

export function getBrandFn(that, selectedOptions, brandModel) {
  // 获取工厂和品牌
  const { dispatch, brandData } = that.props;
  const targetOption = selectedOptions[selectedOptions.length - 1];
  targetOption.loading = true;
  dispatch({
    type: brandModel,
    payload: {
      brandId: targetOption.id
    }
  }).then(() => {
    targetOption.loading = false;
    let { productData } = that.props;
    productData.map(item => {
      item.label = item.name;
      item.value = item.id;
      delete item.name;
      return item;
    });
    targetOption.children = productData;
    let data =  brandData.map(item => {
      if(item.id === targetOption.id) {
        return targetOption;
      }
      return item;
    });
    that.setState({ brandOption: data });
  });
}
